﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using CustomersSuppliersBusinessLayer;
using CustomersSuppliersDataLayer;
using System.Collections;
using WebHelper.Helpers;

namespace CustomersSuppliersWebApplication
{
    public partial class WebFormTypeOfTrades : WebFormBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitializeTelerikRequest(this.RadTextBoxSearch, "onkeyup");
                this.PageTitle = "نوع التجارة";
            }
        }
        protected void RadTextBoxSearch_TextChanged(object sender, EventArgs e)
        {
            this.getTypeOfTrades();
            this.RadGridResults.Rebind();
        }
        protected void RadGridResults_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.getTypeOfTrades();
        }
        protected void RadGridResults_ItemCreated(object sender, GridItemEventArgs e)
        {
            //pager
            if (e.Item is GridPagerItem)
            {
                GridPagerItem item = e.Item as GridPagerItem;
                Button changePageSize = (Button)e.Item.FindControl("ChangePageSizeLinkButton");
                changePageSize.Text = "تغيير";
                Button goTO = (Button)e.Item.FindControl("GoToPageLinkButton");
                goTO.Text = "تغيير";
                Label itesNumberLabel = (Label)e.Item.FindControl("ChangePageSizeLabel");
                itesNumberLabel.Text = "          عدد الصفوف :";
                Label goTOLabel = (Label)e.Item.FindControl("GoToPageLabel");
                goTOLabel.Text = "صفحة :";
                Label ofLabel = (Label)e.Item.FindControl("PageOfLabel");
                ofLabel.Text = "من " + item.Paging.PageCount.ToString();
            }
        }
        protected void RadGridResults_ItemCommand(object sender, GridCommandEventArgs e)
        {
            //set default value
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                //e.Canceled = true;
                //System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                //newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_StartDate] = DateTime.Now.Date;
                //e.Item.OwnerTableView.InsertItem(newValues);
            }

        }
        protected void RadGridResults_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLTypeOfTrade TypeOfTrade = new BLTypeOfTrade();
            if (newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_Code] != null)
            {
                TypeOfTrade.TypeOfTrade_Code = newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_Code].ToString();
            }
            if (newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr] != null && newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr].ToString().Trim() != "")
            {
                TypeOfTrade.TypeOfTrade_NameAr = newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }
            if (newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameEn] != null)
            {
                TypeOfTrade.TypeOfTrade_NameEn = newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameEn].ToString();
            }
            if (newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_Notes] != null)
            {
                TypeOfTrade.TypeOfTrade_Notes = newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_Notes].ToString();
            }
            TypeOfTrade.TypeOfTrade_Search = StringHelper.ToUniqueStatement(TypeOfTrade.TypeOfTrade_Code + TypeOfTrade.TypeOfTrade_NameAr + TypeOfTrade.TypeOfTrade_NameEn + TypeOfTrade.TypeOfTrade_Notes);
            TypeOfTrade.AutoSave();
            if (TypeOfTrade.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLTypeOfTrade TypeOfTrade = new BLTypeOfTrade();
            TypeOfTrade.TypeOfTrade_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLTypeOfTrade.PropertiesNames.TypeOfTrade_ID].ToString());
            TypeOfTrade.LoadByIdentifiers();
            if (newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_Code] != null)
            {
                TypeOfTrade.TypeOfTrade_Code = newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_Code].ToString();
            }
            if (newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr] != null && newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr].ToString().Trim() != "")
            {
                TypeOfTrade.TypeOfTrade_NameAr = newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }
            if (newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameEn] != null)
            {
                TypeOfTrade.TypeOfTrade_NameEn = newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameEn].ToString();
            }
            if (newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_Notes] != null)
            {
                TypeOfTrade.TypeOfTrade_Notes = newValues[BLTypeOfTrade.PropertiesNames.TypeOfTrade_Notes].ToString();
            }
            TypeOfTrade.TypeOfTrade_Search = StringHelper.ToUniqueStatement(TypeOfTrade.TypeOfTrade_Code + TypeOfTrade.TypeOfTrade_NameAr + TypeOfTrade.TypeOfTrade_NameEn + TypeOfTrade.TypeOfTrade_Notes);
            TypeOfTrade.AutoSave();
            if (TypeOfTrade.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            BLTypeOfTrade TypeOfTrade = new BLTypeOfTrade();
            TypeOfTrade.TypeOfTrade_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLTypeOfTrade.PropertiesNames.TypeOfTrade_ID].ToString());
            TypeOfTrade.DeleteByIdentifiers();
            if (TypeOfTrade.Error == null)
            {
                this.ShowNotification("تم الحذف بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحذف", NotificationIconType.Error);
                return;
            }
        }
        protected void RadGridResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //width
                GridEditableItem editedItem = e.Item as GridEditableItem;
                GridEditManager editMan = editedItem.EditManager;
                GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)(editMan.GetColumnEditor(BLTypeOfTrade.PropertiesNames.TypeOfTrade_Notes));
                editor.TextBoxMode = TextBoxMode.MultiLine;
                editor.TextBoxControl.Width = Unit.Pixel(300);
                editor.TextBoxControl.Height = Unit.Pixel(40);
            }
        }

        private void getTypeOfTrades()
        {
            var items = from i in CashVariables.TypeOfTradeCollection
                    where i.TypeOfTrade_Search.Contains(StringHelper.ToUniqueStatement(this.RadTextBoxSearch.Text))
                    select i;
            BLTypeOfTradeCollection TypeOfTradeCollection = new BLTypeOfTradeCollection(items);
            this.RadGridResults.DataSource = TypeOfTradeCollection;
            this.RadGridResults.MasterTableView.DataKeyNames = BLTypeOfTrade.PropertiesNames.Identifiers;
        }
    }
}